<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="utf-8">
    <title>deploy</title>
    <style>
        label {
            width: 10%;
            text-align: right;
            display: inline-block;
        }

        input {
            width: 80%;
            height: 27px;
            margin: 7px;
        }
    </style>
    <script>
        function submit() {
            let submitButton = document.querySelector('button');
            submitButton.disabled = 'disabled';
            let resultPara = document.querySelector('#result');
            let formData = new FormData();
            let key = document.querySelector('input[name="key"]').value;
            let deploy = document.querySelector('input[name="deploy"]').value;
            let deploys = document.querySelector('input[name="deploys"]').value;
            let test = document.querySelector('input[name="test"]').checked;
            formData.append('key', key);
            formData.append('deploy', deploy);
            formData.append('deploys', deploys);
            formData.append('test', test);
            fetch('/deploy', { method: 'POST', body: formData })
                .then(response => response.text())
                .then(text => {
                    resultPara.innerText = text;
                    if (!key) {
                        submitButton.disabled = undefined;
                    } else {
                        fetch('/deploy?key=' + key + '.result', { method: 'GET' })
                            .then(response => response.json())
                            .then(json => { resultPara.innerText = json.result; submitButton.disabled = undefined })
                            .catch(error => { console.log(error); resultPara.innerText = 'error'; submitButton.disabled = undefined })
                    }
                })
                .catch(error => { console.log(error); resultPara.innerText = 'error'; submitButton.disabled = undefined })
        }
    </script>
</head>

<body>
    <label>key</label>
    <input name="key" placeholder="deploy">
    <br />
    <label>deploy</label>
    <input name="deploy" placeholder="namespace service ip">
    <br />
    <label>deploys</label>
    <input name="deploys" placeholder="service [namespace=ip,]*">
    <br />
    <label></label>
    <button onclick="submit()" style="font-size: 18px;">submit</button>
    <input type="checkbox" name="test" checked title="test mode" style="width: auto; vertical-align: middle;">
    <br />
    <p id="result"></p>
</body>

</html>